追码溯源:一个可执行程序的一生

1. 概述

以下列简单程序hello.c为例,追码溯源,深入理解一个可执行程序的一生。

#include <stdio.h>

int main(){
    printf("Hello World!");
    return 0;
}

2. 深入源码分析

深入源码分析一个可执行程序的一生。程序的函数调用关系如下:

printf --> write --> sys_write(系统调用)-->

2.1 printf

printf是C库的一函数,C库更确切的说是叫glibc(The GNU C Library),以下是来自官网的一些介绍:

The GNU C Library project provides the core libraries for the GNU system and GNU/Linux systems, as well as many other systems that use Linux as the kernel. These libraries provide critical APIs including ISO C11, POSIX.1-2008, BSD, OS-specific APIs and more.

The GNU C Library is designed to be a backwards compatible, portable, and high performance ISO C library. It aims to follow all relevant standards including ISO C11, POSIX.1-2008, and IEEE 754-2008.

下载最新稳定版2.37,

git clone https://sourceware.org/git/glibc.git
cd glibc
git checkout release/2.37/master

查看glibc-2.37/stdio-common/printf.c

#include <libioP.h>
#include <stdarg.h>
#include <stdio.h>

#undef printf

/* Write formatted output to stdout from the format string FORMAT.  */
/* VARARGS1 */
int
__printf (const char *format, ...)
{
  va_list arg;
  int done;

  va_start (arg, format);
  done = __vfprintf_internal (stdout, format, arg, 0);
  va_end (arg);

  return done;
}

#undef _IO_printf
ldbl_strong_alias (__printf, printf);
ldbl_strong_alias (__printf, _IO_printf);
本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2023-04-13 17:49

results matching ""

    No results matching ""